<?php

class CotizacionsController extends AppController {

    var $name = 'Cotizacions';
    var $components = array('Search.Prg');

    public $presetVars = array(
            array('field' => 'empresa', 'type' => 'value'));
    
    function index() {
        $this->Cotizacion->recursive = 0;
        $this->Prg->commonProcess();
        $this->paginate['conditions'] = $this->Cotizacion->parseCriteria($this->passedArgs);
        $this->set('cotizacions', $this->paginate());
    }

    function index_admin() {
        $this->Cotizacion->recursive = 0;
        $this->Prg->commonProcess();
        $this->paginate['conditions'] = $this->Cotizacion->parseCriteria($this->passedArgs);
        $this->set('cotizacions', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid cotizacion', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('cotizacion', $this->Cotizacion->read(null, $id));
    }

    function add($idOI = null) {
        if (!$idOI && empty($this->data)) {
            $this->Session->setFlash(__('Seleccione una orden de inspeccion', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            $this->Cotizacion->create();
            $fileData = fread(fopen($this->data['Cotizacion']['File']['tmp_name'], "r"),
                            $this->data['Cotizacion']['File']['size']);

            $this->data['Cotizacion']['archivo_name'] = $this->data['Cotizacion']['File']['name'];
            $this->data['Cotizacion']['archivo_type'] = $this->data['Cotizacion']['File']['type'];
            $this->data['Cotizacion']['archivo_size'] = $this->data['Cotizacion']['File']['size'];
            $this->data['Cotizacion']['archivo_data'] = $fileData;

            if ($this->Cotizacion->save($this->data)) {
                $this->Session->setFlash(__('The cotizacion has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The cotizacion could not be saved. Please, try again.', true));
            }
        }
         $ordenInspeccion = $this->Cotizacion->OrdenInspeccion->findById($idOI);
        $this->data["Cotizacion"]["orden_inspeccion_id"] = $idOI;
        $ordenInspeccions = $this->Cotizacion->OrdenInspeccion->find('list');
        $this->set(compact('ordenInspeccion','ordenInspeccions'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid cotizacion', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            $fileData = fread(fopen($this->data['Cotizacion']['File']['tmp_name'], "r"),
                            $this->data['Cotizacion']['File']['size']);

            $this->data['Cotizacion']['archivo_name'] = $this->data['Cotizacion']['File']['name'];
            $this->data['Cotizacion']['archivo_type'] = $this->data['Cotizacion']['File']['type'];
            $this->data['Cotizacion']['archivo_size'] = $this->data['Cotizacion']['File']['size'];
            $this->data['Cotizacion']['archivo_data'] = $fileData;
            if ($this->Cotizacion->save($this->data)) {
                $this->Session->setFlash(__('The cotizacion has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The cotizacion could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Cotizacion->read(null, $id);
        }
        $ordenInspeccions = $this->Cotizacion->OrdenInspeccion->find('list');
        $this->set(compact('ordenInspeccions'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for cotizacion', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Cotizacion->delete($id)) {
            $this->Session->setFlash(__('Cotizacion deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Cotizacion was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function download($id) {
    //Configure::write('debug', 0);
    $file = $this->Cotizacion->findById($id);

    header('Content-type: ' . $file['Cotizacion']['archivo_type']);
    header('Content-length: ' . $file['Cotizacion']['archivo_size']); // some people reported problems with this line (see the comments), commenting out this line helped in those cases
    header('Content-Disposition: attachment; filename="'.$file['Cotizacion']['archivo_name'].'"');
    echo $file['Cotizacion']['archivo_data'];

    exit();
}


}
?>